package cn.parllay.dingdangproject.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.parllay.dingdangproject.R;
import cn.parllay.dingdangproject.bean.customer.VipCustomer;
import cn.parllay.dingdangproject.utils.DateUtils;
import cn.parllay.dingdangproject.utils.DensityUtil;
import cn.parllay.dingdangproject.utils.GlideUtils;

/**
 * Created by sunny on 2017/8/26.
 */

public class VipFragmentAdapter extends RecyclerView.Adapter<VipFragmentAdapter.VipViewHolder> implements View.OnClickListener {

    private static final String TAG = "VipFragmentAdapter";
    private Context mContext = null;
    private List<VipCustomer> mVipCustomerList = new ArrayList<>();

    public VipFragmentAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public VipViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_vip_customers, parent, false);
        view.setOnClickListener(this);
        return new VipViewHolder(view);
    }


    @Override
    public void onBindViewHolder(VipViewHolder holder, int position) {

        String contactId = mVipCustomerList.get(position).getContactId();
        holder.itemView.setTag(contactId);

        bindData(holder, position);
    }

    @Override
    public int getItemCount() {
        return mVipCustomerList == null ? 0 : mVipCustomerList.size();
    }

    //刷新方法
    public void refresh(List<VipCustomer> vipCustomerList) {

        if (vipCustomerList != null && vipCustomerList.size() >= 0) {
            mVipCustomerList.clear();
            mVipCustomerList.addAll(vipCustomerList);
            notifyDataSetChanged();
        }
    }

    //加载更多方法
    public void loadMore(List<VipCustomer> vipCustomerList) {
        if (vipCustomerList != null && vipCustomerList.size() >= 0) {
            mVipCustomerList.addAll(vipCustomerList);
            notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        String contactId = (String) v.getTag();
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, contactId);
        }
    }


    static class VipViewHolder extends RecyclerView.ViewHolder {

        private ImageView iv_userPhoto;
        private TextView tv_customerName;
        private TextView tv_lastTime;
        private LinearLayout ll_tags;

        public VipViewHolder(View itemView) {
            super(itemView);

            iv_userPhoto = (ImageView) itemView.findViewById(R.id.iv_userPhoto);
            tv_customerName = (TextView) itemView.findViewById(R.id.tv_customerName);
            tv_lastTime = (TextView) itemView.findViewById(R.id.tv_lastTime);
            ll_tags = (LinearLayout) itemView.findViewById(R.id.ll_tags);
        }
    }

    private void bindData(VipViewHolder holder, int position) {

        VipCustomer vipCustomer = mVipCustomerList.get(position);
        String customerName = vipCustomer.getCustomerName() == null ? "": vipCustomer.getCustomerName();
        holder.tv_customerName.setText(customerName);
        String lastDateArrived= vipCustomer.getLastDateArrived();
        lastDateArrived = lastDateArrived == null ? "" :lastDateArrived;
        if(!"".equals(lastDateArrived)) {
            lastDateArrived = DateUtils.removeSecond(vipCustomer.getLastDateArrived());
        }

        holder.tv_lastTime.setText("上次进店:" + lastDateArrived);
        String tags = vipCustomer.getTags();
            addTextViewsByTags(holder.ll_tags, tags);
        GlideUtils.loadCircleImage(mContext, GlideUtils.checkUrl(vipCustomer.getHeadImage()), holder.iv_userPhoto);

    }

    private OnItemClickListener mOnItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(View view, String contactId);
    }

    public void setmOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }


    private void addTextViewsByTags(LinearLayout linearLayout, String tagString) {

        linearLayout.removeAllViews();

        if(tagString == null || "".equals(tagString)) {
            return;
        }
        List<String> tags = splitTags(tagString);
        for (int i = 0; i < tags.size(); i++) {
            TextView textView = new TextView(mContext);
            textView.setTextColor(mContext.getResources().getColor(R.color.black1));
            textView.setBackgroundResource(R.drawable.shape_tags_normal_green);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i > 0) {
                layoutParams.leftMargin = DensityUtil.dip2px(mContext, 5);
            }
            int padding = DensityUtil.dip2px(mContext, 3);
            textView.setPadding(padding, padding, padding, padding);
            textView.setText(tags.get(i));
            linearLayout.addView(textView, layoutParams);

        }
    }
    private List<String> splitTags(String tags) {

        String[] split = tags.split("\\^");
        return Arrays.asList(split);
    }

}
